/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-22     yc       the first version
 */
#include "bsp.h"

ADC_HandleTypeDef hadc3 ={0};
DMA_HandleTypeDef hdma_adc3 = {0};


/**
* @brief ADC MSP Initialization
* This function configures the hardware resources used in this example
* @param hadc: ADC handle pointer
* @retval None
*/
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hadc->Instance==ADC3)
  {
  /* USER CODE BEGIN ADC3_MspInit 0 */

  /* USER CODE END ADC3_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_ADC3_CLK_ENABLE();

    SOLAR_VOLT_ADC_CLK_EN();
    BAT_VOLT_ADC_CLK_EN();
    MOTOR_V_CUR_ADC_CLK_EN();
    MOTOR_H_CUR_ADC_CLK_EN();

    /**ADC3 GPIO Configuration
    PF3     ------> ADC3_IN9
    PF4     ------> ADC3_IN14
    PF5     ------> ADC3_IN15
    PF6     ------> ADC3_IN4
    PF7     ------> ADC3_IN5
    PF8     ------> ADC3_IN6
    */
    GPIO_InitStruct.Pin = SOLAR_VOLT_ADC_GPIO;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(SOLAR_VOLT_ADC_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = BAT_VOLT_ADC_GPIO ;
    HAL_GPIO_Init(BAT_VOLT_ADC_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = MOTOR_V_CUR_ADC_GPIO ;
    HAL_GPIO_Init(MOTOR_V_CUR_ADC_PORT, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = MOTOR_H_CUR_ADC_GPIO ;
    HAL_GPIO_Init(MOTOR_H_CUR_ADC_PORT, &GPIO_InitStruct);



    /* ADC3 DMA Init */
    /* ADC3 Init */
    __HAL_RCC_DMA2_CLK_ENABLE();
    hdma_adc3.Instance = DMA2_Stream0;
    hdma_adc3.Init.Channel = DMA_CHANNEL_2;
    hdma_adc3.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_adc3.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_adc3.Init.MemInc = DMA_MINC_ENABLE;
    hdma_adc3.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
    hdma_adc3.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    hdma_adc3.Init.Mode = DMA_CIRCULAR;
    hdma_adc3.Init.Priority = DMA_PRIORITY_LOW;
    hdma_adc3.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

    if (HAL_DMA_Init(&hdma_adc3) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc3);

  /* USER CODE BEGIN ADC3_MspInit 1 */

  /* USER CODE END ADC3_MspInit 1 */
  }

}






void bsp_adc_Init(void)
{
    /* USER CODE BEGIN ADC3_Init 0 */

     /* USER CODE END ADC3_Init 0 */

     ADC_ChannelConfTypeDef sConfig = {0};

     /* USER CODE BEGIN ADC3_Init 1 */

     /* USER CODE END ADC3_Init 1 */
     /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
     */
     hadc3.Instance = ADC3;
     hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
     hadc3.Init.Resolution = ADC_RESOLUTION_12B;
     hadc3.Init.ScanConvMode = ENABLE;
     hadc3.Init.ContinuousConvMode = ENABLE;
     hadc3.Init.DiscontinuousConvMode = DISABLE;
     hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
     hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
     hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
     hadc3.Init.NbrOfConversion = 4;
     hadc3.Init.DMAContinuousRequests = ENABLE;
     hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
     if (HAL_ADC_Init(&hadc3) != HAL_OK)
     {
       Error_Handler();
     }
     /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
     */
     sConfig.Channel = SOLAR_VOLT_ADC_CHANNEL;
     sConfig.Rank = 1;
     sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
     if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
     {
       Error_Handler();
     }
     /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
     */
     sConfig.Channel = BAT_VOLT_ADC_CHANNEL;
     sConfig.Rank = 2;
     if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
     {
       Error_Handler();
     }
     sConfig.Channel = MOTOR_V_CUR_ADC_CHANNEL;
     sConfig.Rank = 3;
     if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
     {
       Error_Handler();
     }
     sConfig.Channel = MOTOR_H_CUR_ADC_CHANNEL;
     sConfig.Rank = 4;
     if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
     {
       Error_Handler();
     }


     /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.*/

     /* USER CODE BEGIN ADC3_Init 2 */

     HAL_ADC_Start_DMA(&hadc3, (uint32_t*)Adc_Buffer, ADC_SAMPLE_VALUE);
     /* USER CODE END ADC3_Init 2 */

}
